package com.itheima.ydd.service.impl;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.ydd.entity.Result;
import com.itheima.ydd.entity.SysConfig;
import com.itheima.ydd.entity.sysconfig.*;
import com.itheima.ydd.mapper.SysConfigMapper;
import com.itheima.ydd.service.SysConfigService;
import org.junit.Test;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Wang.XL
 */
@Service
public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig> implements SysConfigService {

    @Autowired
    private SysConfigMapper configMapper;

    /**
     * 得到所有配置
     *
     * @return
     */
    @Override
    public Result allConfig() {
        //1.先得到configRules
        ConfigRules configRules = new ConfigRules();
        //1.1利用site_name去表中查询validator
        SysConfig config = this.getOne(new LambdaQueryWrapper<SysConfig>().eq(
                SysConfig::getAttr_key, "site_name"
        ));
        //1.2configRules赋值
        configRules.setSite_name(config.getValidator());

        //2.得到configForm对象
        //2.1创建map集合
        Map<String, String> configFormMap = new HashMap<>();
        //2.2得到attr_key和attr_value
        List<SysConfig> sysConfigs = this.list();
        for (SysConfig sysConfig : sysConfigs) {
            String attrKey = sysConfig.getAttr_key();
            String attrValue = sysConfig.getAttr_value();
            configFormMap.put(attrKey, attrValue);
        }

        //3.得到ConfigAllData集合
        List<ConfigAllData> allDataList = sysConfigs.stream()
                .map(sysConfig -> {
                    ConfigAllData configAllData = new ConfigAllData();
                    BeanUtils.copyProperties(sysConfig, configAllData,
                            "group_value", "tab_value", "attr_name", "del_flag",
                            "placeholder", "can_delete", "sort_num");
                    return configAllData;
                })
                .collect(Collectors.toList());

        //4.获取list集合
        //4.1拿到--name=基础配置==> "key":"base"==>List<children>
        //模拟创建一个集合
        List<TableValue> tabValue = new ArrayList<>();
        TableValue tableField1 = new TableValue();
        tableField1.setKey("base");
        tableField1.setName("基础配置");
        TableValue tableField2 = new TableValue();
        tableField2.setKey("system");
        tableField2.setName("系统设置");
        TableValue tableField3 = new TableValue();
        tableField3.setKey("custom");
        tableField3.setName("其他设置");
        tabValue.add(tableField1);
        tabValue.add(tableField2);
        tabValue.add(tableField3);
        //1.获取表中有集中分组
        List<String> groupVs = configMapper.getGroupValue();
        List<ConfigParent> parentList = getList(tabValue, groupVs);

        //由上面得到的数据进行实体类的封装
        SysAllConfig sysAllConfig = new SysAllConfig();
        sysAllConfig.setList(parentList);
        sysAllConfig.setConfigForm(configFormMap);
        sysAllConfig.setConfigRules(configRules);
        sysAllConfig.setAllData(allDataList);

        return new Result(0, "", sysAllConfig);
    }

    /**
     * 得到总数据list
     *
     * @param tabValue
     * @param groupVs
     * @return
     */
    private List<ConfigParent> getList(List<TableValue> tabValue, List<String> groupVs) {
        //1.创建一个List<ConfigParent>
        List<ConfigParent> parentList = new ArrayList<>();
        //2.利用基础设置进行遍历
        for (TableValue tableValue : tabValue) {
            ConfigParent configParent = new ConfigParent();
            String key = tableValue.getKey();
            configParent.setKey(key);
            String name = tableValue.getName();
            configParent.setName(name);

            //创建一个childrenlist存放children
            List<ConfigChildren> childrenList = new ArrayList<>();

            //创建children对象
            for (String groupV : groupVs) {
                ConfigChildren children = new ConfigChildren();
                children.setName(groupV);
                //利用key和groupValue去查找
                //设置条件
                LambdaQueryWrapper<SysConfig> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(SysConfig::getTab_value, tableValue.getKey())
                        .eq(SysConfig::getGroup_value, groupV);
                List<SysConfig> sysConfigs = this.list(wrapper);
                 if (sysConfigs.size() > 0) {
                children.setData(sysConfigs);
                childrenList.add(children);
                 }
            }
            //将得到的childrenList赋值给configParent
            configParent.setChildren(childrenList);
            parentList.add(configParent);
        }
        return parentList;
    }
}
